package com.suoyue.store.service.impl;

import com.suoyue.store.entity.Product;
import com.suoyue.store.entity.User;
import com.suoyue.store.mapper.ProductMapper;
import com.suoyue.store.mapper.UserMapper;
import com.suoyue.store.service.ex.ServiceException;
import com.suoyue.store.service.ex.UsernameDuplicatedException;
import com.suoyue.store.service.iProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements iProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void insert(Product product) {
        Integer rows=productMapper.insertProduct(product.getName(),product.getDescription(),product.getPrice(),product.getImage());
        if(rows!=1){
            throw new ServiceException("添加失败,存在未知异常");
        }
    }

    @Override
    public Product findProductByPid(Integer pid) {
        Product product=productMapper.findProductByPid(pid);
//        System.out.println(product);
        if(product==null){
            throw new ServiceException("pid没有对应商品");
        }
        return product;
    }

    @Override
    public List<Product> getProductListBypage(Integer page) {

        List<Product> result=productMapper.getProductListByPage(page*8);
        if(result.size()==0){
            throw new ServiceException("page无效");
        }

        return result;
    }

    @Override
    public Integer getProductNum() {

        return productMapper.getProductNum();
    }

    @Override
    public Integer deleteProduct(Integer pid) {
        Integer rows=productMapper.deleteProduct(pid);
        if(rows!=1){
            throw new ServiceException("添加失败,存在未知异常");
        }
        return rows;
    }

    @Override
    public Integer updateProductByPid(Integer pid, String name, String description, Double price, String image) {
        Integer rows=productMapper.updateProductByPid(pid, name, description, price, image);
        if(rows!=1){
            throw new ServiceException("添加失败,存在未知异常");
        }
        return rows;
    }

}
